//
//  TestViewController.swift
//  swift_learning
//
//  Created by aron on 2017/2/4.
//  Copyright © 2017年 arons. All rights reserved.
//

import UIKit

class ActionCellModel {
    var actionName : String?
    var actionCallBack : (() -> Void)?
    
    init(actionName: String, actionCallBack : @escaping (() -> Void)) {
        self.actionCallBack = actionCallBack
        self.actionName = actionName
    }
}

class TestViewController: UITableViewController {

    lazy var actionCellModels = [ActionCellModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 注册Cell
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "UITableViewCell")
        
        // 添加数据
        addModel(model: ActionCellModel(actionName: "TestXib", actionCallBack: { [weak self] in
            self?.navigationController?.pushViewController(TestXibViewController(), animated: true)
        }))
        
        // 添加数据
        addModel(model: ActionCellModel(actionName: "TestStoryboard", actionCallBack: { [weak self] in
            self?.navigationController?.pushViewController(TestStoryboardViewController(), animated: true)
        }))
    }
    
    private func addModel(model : ActionCellModel) {
        actionCellModels.append(model)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSections(in tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return actionCellModels.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "UITableViewCell")!
        
        // 数据
        let model = actionCellModels[indexPath.row]
        cell.textLabel?.text = model.actionName
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = actionCellModels[indexPath.row]
        model.actionCallBack!()
    }

}
